using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;

public class Bubbles : MonoBehaviour
{
    // [SerializeField] private ParticleSystem bubble;
    // private GameObject player => GameObject.FindGameObjectWithTag("Player");
    // private List<ParticleSystem.Particle> _enter = new List<ParticleSystem.Particle>();
    // private List<ParticleSystem.Particle> _exit = new List<ParticleSystem.Particle>();
    // [SerializeField]private GameObject bubbles;
    // private List<GameObject> _stayBubbles = new List<GameObject>();
    // private Transform _faceAt;
    //
    // private void Awake()
    // {
    //     bubble = GetComponent<ParticleSystem>();
    //     _faceAt = player.transform.GetChild(1);
    // }
    //
    // private void Start()
    // {
    //     bubbles.GetComponent<Renderer>().material = bubble.GetComponent<Renderer>().material;
    // }
    //
    // public float ReleaseSkill()
    // {
    //     // transform.position = new Vector3(2 * player.transform.position.x, (player.GetComponent<CapsuleCollider2D>().size.y / 3) * 2, 0);
    //     transform.position = _faceAt.position;
    //     bubble.Play();
    //     return bubble.main.duration;
    // }
    //
    // private void OnParticleTrigger()
    // {
    //     int numEnter = bubble.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, _enter);
    //     int exitNumber = bubble.GetTriggerParticles(ParticleSystemTriggerEventType.Exit, _exit);
    //
    //     for (int i = 0; i < numEnter; i++)
    //     {
    //         ParticleSystem.Particle p = _enter[i];
    //         p.startColor = new Color32(255, 0, 0, 255);
    //         var stayBubble = Instantiate(bubbles, p.position, Quaternion.identity);
    //         _stayBubbles.Add(stayBubble);
    //         _enter[i] = p;
    //     }
    //
    //     // for (int i = 0; i < exitNumber; i++)
    //     // {
    //     //     ParticleSystem.Particle p = _exit[i];
    //     //     p.startColor = new Color32(0, 255, 0, 255);
    //     //     _exit[i] = p;
    //     // }
    //     
    //     bubble.SetTriggerParticles(ParticleSystemTriggerEventType.Enter, _enter);
    //     // bubble.SetTriggerParticles(ParticleSystemTriggerEventType.Exit, _exit);
    // }
    
    [Header("泡泡速度")]
    [SerializeField]private float speed;
    private GameObject player => GameObject.FindWithTag("Player");
    private Vector2 startDir;
    private bool touchNoInteract;
    private CircleCollider2D coll => GetComponent<CircleCollider2D>();
    [SerializeField] private LayerMask layerMask;
    [Header("判定范围")]
    [SerializeField] private float gizmosRadius;

    private void Awake()
    {
        // coll = GetComponent<CircleCollider2D>();
    }

    private void Start()
    {
        startDir = player.transform.localScale.normalized;
        SkillManger.Instance.isReleaseSkill = true;
    }
    

    private void FixedUpdate()
    {
        Move();
    }
    
    private void Move()
    {
        transform.position += new Vector3(speed * startDir.x, 0, 0);
    }

    private void OnTriggerStay2D(Collider2D other)
    {

        if (other.CompareTag("Can Fly"))
        {
            speed = 0;
            transform.position = other.transform.position;
            Destroy(gameObject);
            SkillManger.Instance.isReleaseSkill = false;
        }
        else if(other.CompareTag("Player")){}
        else if(other.CompareTag("Untagged")) 
        {
            Destroy(gameObject);
            SkillManger.Instance.isReleaseSkill = false;
        }
    }

    private void CheckTrigger()
    {
        touchNoInteract = Physics2D.OverlapCircle(transform.position, gizmosRadius, layerMask);
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.DrawWireSphere((Vector2)transform.position, gizmosRadius);
    }

    // private IEnumerator MoveToInteract(Vector3 target)
    // {
    //
    // }
}
